﻿<?php
	session_start();
	require_once('../config.php');
	
	if(!isset($_SESSION['logon']))
	{
		header("Location: ". $config_basedir. "/WKSCP/login.php?url=nhom_CMS");
		exit();
	}

	require_once '../model.php';
	
	
	
	if (isset($_GET['action']) == true){
		// $id_nhom = null;
		if (isset($_GET['id']) == true){
			$error = 0;
			if (is_numeric($_GET['id']) == false){
				$error = 1;
			}
			
			if ($error == 1){
				header("Location: ". $config_basedir. "/WKSCP/nhom_CMS.php");		
			}
			else{
				$id_nhom = $_GET['id'];
			}
		}
	
		$action = $_GET['action'];
		
		if($action == 'LIST'){
			$nhom = GetNhom();
			//Xu ly
			require_once('template/nhom/nhom_view_CMS.phtml');
			exit();
		}
		
		if($action == 'ADD'){
			if(!isset($_POST['submit'])){
				require_once('template/nhom/nhom_add_CMS.phtml');
				exit();
			}
			else{
				if($_POST['ten_nhom'] != '')
				{
					InsertNhom($_POST['ten_nhom']);
					header("Location: ". $config_basedir. "/WKSCP/nhom_CMS.php?action=LIST");
				}
			}
		}
		
		if($action == 'EDIT'){
			if (!isset($id_nhom))
				header("Location: ". $config_basedir. "/WKSCP/nhom_CMS.php");		
				
			if(!isset($_POST['submit'])){
				$nhom = GetNhomById($id_nhom);
				require_once('template/nhom/nhom_edit_CMS.phtml');
				exit();
			}
			else{
				if($_POST['ten_nhom'] != ''){
					EditNhom($id_nhom, $_POST['ten_nhom']);
					header("Location: ". $config_basedir. "/WKSCP/nhom_CMS.php?action=LIST");
				}
			}
		}
		
		if($action == 'DEL'){
			if (isset($id_nhom))
				DeleteNhom($id_nhom);
				
			header("Location: ". $config_basedir. "/WKSCP/nhom_CMS.php");		
		}
	}
	else{
		$nhom = GetNhom();
		//Xu ly
		require_once('template/nhom/nhom_view_CMS.phtml');
	}
?>